package com.adcc.plist;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class Test {
	
	public static void main(String[] args){
		try {
			createPlist();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 自动生成plist文件方法
	 * 
	 * @param domainName服务器地址
	 * @param versionNameipa应用名字
	 * @param appPath存储目录
	 * @throws IOException
	 */
	public static void createPlist() throws IOException {
		String appPath = "/Users/aaronwf/Document";
		String domainName = "EFBEnroute";
		String versionName = "Ver1.0";
		// plist文件链接服务器地址
		StringBuffer allPath = new StringBuffer();
		allPath.append(domainName);
		allPath.append("?sAction=download&amp;method=download&amp;fileName=");
		allPath.append(versionName);
		allPath.append("&amp;fileCate=appDownload&amp;filePath=");
		allPath.append(appPath);
		// plist文件存储目录
		String filePath = "/Users/aaronwf/Documents/test.plist";

		// 创建文档类型
		DocType docType = new DocType("plist");
		docType.setPublicID("-//Apple//DTD PLIST 1.0//EN");
		docType.setSystemID("http://www.apple.com/DTDs/PropertyList-1.0.dtd");
		// 创建根节点 plist
		Element root = new Element("plist");
		root.setAttribute("version", "1.0");
		//
		Element rootDict = new Element("dict");
		rootDict.addContent(new Element("key").setText("items"));
		Element rootDictArray = new Element("array");
		Element rootDictArrayDict = new Element("dict");
		rootDictArrayDict.addContent(new Element("key").setText("assets"));

		Element rootDictArrayDictArray = new Element("array");
		Element rootDictArrayDictArrayDict1 = new Element("dict");
		rootDictArrayDictArrayDict1.addContent(new Element("key")
				.setText("kind"));
		rootDictArrayDictArrayDict1.addContent(new Element("string")
				.setText("software-package"));
		rootDictArrayDictArrayDict1.addContent(new Element("key")
				.setText("url"));
		rootDictArrayDictArrayDict1.addContent(new Element("string")
				.setText(allPath.toString()));

		Element rootDictArrayDictArrayDict2 = new Element("dict");
		rootDictArrayDictArrayDict2.addContent(new Element("key")
				.setText("kind"));
		rootDictArrayDictArrayDict2.addContent(new Element("string")
				.setText("display-image"));
		rootDictArrayDictArrayDict2.addContent(new Element("key")
				.setText("needs-shine"));
		rootDictArrayDictArrayDict2.addContent(new Element("true"));
		rootDictArrayDictArrayDict2.addContent(new Element("key")
				.setText("url"));
		rootDictArrayDictArrayDict2.addContent(new Element("string")
				.setText(domainName.replace("house", "icon/eims.png")));

		rootDictArrayDictArray.addContent(rootDictArrayDictArrayDict1);
		rootDictArrayDictArray.addContent(rootDictArrayDictArrayDict2);
		rootDictArrayDict.addContent(rootDictArrayDictArray);
		rootDictArrayDict.addContent(new Element("key").setText("metadata"));

		Element rootDictArrayDictDict = new Element("dict");
		rootDictArrayDictDict.addContent(new Element("key")
				.setText("bundle-identifier"));
		rootDictArrayDictDict.addContent(new Element("string")
				.setText("com.cntaiping.tppension.xxzc.iphone"));
		rootDictArrayDictDict.addContent(new Element("key")
				.setText("bundle-version"));
		rootDictArrayDictDict.addContent(new Element("string").setText("1.0"));
		rootDictArrayDictDict.addContent(new Element("key").setText("kind"));
		rootDictArrayDictDict.addContent(new Element("string")
				.setText("software"));
		rootDictArrayDictDict.addContent(new Element("key").setText("title"));
		rootDictArrayDictDict.addContent(new Element("string").setText("项目名称"));
		rootDictArrayDict.addContent(rootDictArrayDictDict);

		rootDictArray.addContent(rootDictArrayDict);
		rootDict.addContent(rootDictArray);
		root.addContent(rootDict);
		// 根节点添加到文档中;
		Document Doc = new Document(root, docType);
		Format format = Format.getPrettyFormat();
		XMLOutputter XMLOut = new XMLOutputter(format);
		// 输出 user.xml 文件；
		FileOutputStream fos = new FileOutputStream(new File(filePath));
		XMLOut.output(Doc, fos);
		// XMLOut.output(Doc);
	}
}
